<template>
  <div class="cartlist">
    <div class="discount">
      <div class="free">
        <van-tag type="warning">免邮</van-tag>
        <span>已满足包邮条件</span>
      </div>
      <div class="free">
        <van-tag type="warning">免邮</van-tag>
        <span>已满足包邮条件</span>
      </div>
    </div>
    <div class="cartprod">
      <div class="item" v-for="(item,i) in list" :key="item.pid">
        <van-checkbox
          @click="choice(i)"
          v-model="checked[i]"
          shape="square"
          checked-color="#07c160"
        />
        <div class="prod">
          <div class="pimg" v-tap="{methods:detail,id:item.pid}">
            <img :src="item.pimg" />
          </div>
          <div class="pdes">
            <p class="pname">{{item.pname}}</p>
            <p class="pdesc">{{item.pdesc}}</p>
            <p class="pprice">￥{{item.pprice}}</p>
          </div>
          <div class="num">×{{value[i]}}</div>
          <van-stepper v-model="value[i]" @plus="plus(i)" @minus="minus(i)" @blur="blur(i)" />
        </div>
      </div>
    </div>
    <van-submit-bar :price="allprice" :button-text="btntxt" @submit="onSubmit">
      <van-checkbox
        v-model="checked2"
        v-tap="{methods:select}"
        shape="square"
        checked-color="#07c160"
      >全选</van-checkbox>
    </van-submit-bar>
  </div>
</template>

<script>
import * as api from "../api/User";
import { Dialog } from "vant";

export default {
  name: "cartlist",
  data() {
    return {
      checked: [],
      checked2: true,
      list: [],
      data: [],
      value: [],
      btntxt: "提交订单",
      allprice: 0
    };
  },
  props: ["status"],
  methods: {
    onSubmit() {
      if (this.btntxt == "删除所选") {
        if (!this.checked2) {
          for (var i = 0; i < this.checked.length; i++) {
            if (this.checked[i]) {
              api.deleteCart({ pid: this.list[i].pid }).then(data => {
                Dialog.alert({
                  message: data.data.msg
                });
              });
              this.list.splice(i, 1);
            }
            this.checked[i] = false;
          }
        } else {
          for (let i = 0; i < this.list.length; i++) {
            api
              .deleteCart({ pid: this.list[i].pid })
              .then(data => {
                // console.log(data);
                // console.log(i);
              })
              .then(() => {
                // console.log(i);
                if (i == this.list.length - 1) {
                  Dialog.alert({
                    message: "删除成功",
                  });
                  this.list = [];
                }
              });
          }
        }
      } else if (this.btntxt == "提交订单") {
      }
    },
    select() {
      this.checked2 = !this.checked2;
      for (var i = 0; i < this.checked.length; i++) {
        this.checked[i] = this.checked2;
      }
      if (this.checked2) {
        for (var i = 0; i < this.checked.length; i++) {
          this.allprice += this.list[i].pprice * this.list[i].cnum * 100;
        }
      } else {
        this.allprice = 0;
      }
    },
    choice(index) {
      this.checked[index] = !this.checked[index];
      var num = 0;
      for (var i = 0; i < this.checked.length; i++) {
        if (this.checked[i]) {
          num++;
        }
      }
      if (num == this.checked.length) {
        this.checked2 = true;
      } else {
        this.checked2 = false;
      }

      if (this.checked[index]) {
        this.allprice += this.list[index].pprice * this.list[index].cnum * 100;
      } else {
        this.allprice -= this.list[index].pprice * this.list[index].cnum * 100;
      }
    },
    plus(index) {
      var cnumber = this.value[index] + 1;
      api.updateCart({ pid: this.list[index].pid, cnumber: cnumber });
    },
    minus(index) {
      var cnumber = this.value[index] - 1;
      api.updateCart({ pid: this.list[index].pid, cnumber: cnumber });
    },
    blur(index) {
      var cnumber = this.value[index] < 1 ? 1 : this.value[index];
      api.updateCart({ pid: this.list[index].pid, cnumber: cnumber });
    },
    detail(id) {
      this.$router.push("/ProdDetail?id=" + id.id);
    }
  },
  mounted() {
    api.getCart().then(data => {
      // console.log(data);
      this.list = data.data.result;
      for (var i = 0; i < this.list.length; i++) {
        this.checked.push(true);
        this.value.push(this.list[i].cnum);
        this.allprice += this.list[i].pprice * this.list[i].cnum * 100;
      }
    });
  },
  watch: {
    status(newValue) {
      var num = document.getElementsByClassName("pprice").length;
      if (newValue) {
        for (var i = 0; i < num; i++) {
          document.getElementsByClassName("pprice")[i].style.display = "none";
          document.getElementsByClassName("num")[i].style.display = "none";
          document.getElementsByClassName("van-stepper")[i].style.display =
            "block";
          this.checked[i] = false;
        }
        document.getElementsByClassName(
          "van-submit-bar__price"
        )[0].style.display = "none";
        this.btntxt = "删除所选";
        this.checked2 = false;
      } else {
        for (var i = 0; i < num; i++) {
          document.getElementsByClassName("pprice")[i].style.display = "block";
          document.getElementsByClassName("num")[i].style.display = "block";
          document.getElementsByClassName("van-stepper")[i].style.display =
            "none";
          this.checked[i] = true;
        }
        document.getElementsByClassName(
          "van-submit-bar__price"
        )[0].style.display = "block";
        this.btntxt = "确认下单";
        this.checked2 = true;
      }
    },
    list(newValue) {
      if (newValue.length == 0) {
        this.$router.push("/guesslike");
      }
    }
  }
};
</script>

<style>
.van-ellipsis {
  font-size: 30px;
}
.cartlist {
  background: rgb(247, 247, 247);
  height: auto;
}
.discount {
  border-top: solid 2px #eee;
  background: #fff;
}
.discount div {
  border-bottom: solid 1px #eee;
}
.discount .free {
  padding: 25px 30px;
}
.discount .free .van-tag {
  margin-right: 30px;
}
.cartprod {
  padding-bottom: 230px;
}
.item {
  padding: 15px 15px 15px 30px;
  margin-top: 20px;
  background: #fff;
  overflow: hidden;
}
.prod {
  float: left;
  background: #fff;
  width: calc(100% - 50px);
  position: relative;
}
.prod .pimg {
  width: 156px;
  margin-left: 20px;
  margin-right: 15px;
  float: left;
}
.prod .pimg img {
  width: 100%;
}
.prod .pdes {
  float: left;
  margin-left: 10px;
  width: 380px;
}
.prod .pdes p {
  margin: 5px 0;
}
.prod .pdes .pname {
  font-size: 30px;
}
.prod .pdes .pdesc {
  color: #a0a0a0;
  font-size: 24px;
}
.prod .pdes .pprice {
  margin-top: 40px;
  font-size: 28px;
}
.prod .num {
  float: left;
  position: absolute;
  right: 10px;
  bottom: 10px;
}
.van-stepper {
  margin-top: 100px;
  margin-right: 0;
  float: right;
  display: none;
}
.close {
  position: absolute;
  top: 20px;
  right: 20px;
}
.close i {
  font-size: 26px;
}
.van-checkbox {
  float: left;
  margin-left: 40px;
}
.item .van-checkbox {
  margin-top: 70px;
  margin-left: -10px;
  margin-right: 20px;
}
.van-button {
  background-color: #000;
  color: #fff;
  border: #000 solid 1px;
}
.van-submit-bar {
  margin-bottom: 100px;
}

.van-submit-bar__text span:nth-of-type(1) {
  display: none;
}
.van-submit-bar__price {
  color: #333;
}
</style>